package com.css.dzj.zfjg.sys.user.service.impl;


import com.css.dzj.zfjg.common.util.Constant;
import com.css.dzj.zfjg.sys.user.entity.QUser;
import com.css.dzj.zfjg.sys.user.entity.User;
import com.css.dzj.zfjg.sys.user.mapper.UserMapper;
import com.css.dzj.zfjg.sys.user.service.UserService;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

;


/**
 * 用户Service接口实现
 * @author liyuan, liangXG
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JPAQueryFactory jpaQueryFactory;

    @Override
    public void save(User user) {
        userMapper.save(user);
    }

    @Override
    public void deleteUserByIds(String[] userIds) {

    }

    @Override
    public void updateUser(User user) {
        // 根据id修改
        userMapper.save(user);
    }

    @Override
    public Boolean updatePassword(String userId, String password, String newPassword) {
        return null;
    }

    @Override
    public User queryUserById(String userId) {
        return null;
    }

    /***
     * 根据用户名和密码查询用户
     * @param userName
     * @param password
     * @return
     */
    @Override
    public User findUserByNameAndPwd(String userName, String password) {
        QUser qUser = QUser.user;
        User user = jpaQueryFactory.selectFrom(qUser).
                where(qUser.userName.eq(userName),
                        qUser.password.eq(password), qUser.delFlag.eq(Constant.NO_DELETE)).fetchOne();
        return user;
    }

    @Override
    public Boolean isUserExist(String userName) {
        QUser qUser = QUser.user;
        // 查询到的行数
        Long count = jpaQueryFactory.selectFrom(qUser)
                .where(qUser.userName.eq(userName), qUser.delFlag.eq(Constant.NO_DELETE))
                .fetchCount();
        return count > 0;
    }
}
